# -*- coding: utf-8 -*-
import unittest
from api.base_api import settings
import json
import requests



# resp.text返回的是Unicode型的数据。resp.content返回的是bytes型也就是二进制的数据。
# 如果你想取文本，可以通过r.text。
# 如果想取图片，文件，则可以通过r.content。
# （resp.json()返回的是json格式数据）

# json.loads()把Json格式字符串解码转换成Python对象


class BaseApi(unittest.TestCase):
    url = ""

    def get(self, url, payload):
        self.response = requests.get(url=url, data=payload, headers=settings.HEADERS)
        return self.response

    def get_message(self):
        if self.response:
            return json.loads(self.response.text)['message']     #转换unicode

    def get_status_code(self):
        if self.response:
            return self.response.json()['status_code']

    def post(self, url, payload, hander=None):
        if hander:
            amend_payload = json.dumps(payload, ensure_ascii=False).encode("utf-8")
            self.response = requests.post(url, data=amend_payload, headers=hander)
        else:
            self.response = requests.post(url, json=payload, headers=settings.HEADERS)
        return self.response

    def put(self, url, payload, hander=None):
        if hander:
            amend_payload = json.dumps(payload, ensure_ascii=False).encode("utf-8")
            self.response = requests.put(url, data=amend_payload, headers=hander)
        else:
            self.response = requests.put(url, json=payload, headers=settings.HEADERS)
        return self.response

    

    # def payload(self, data):
    #     return {
    #         "mobile": data['mobile'],
    #         "verify_code": data['verify_code'],
    #         "password": data['password']
    #     }

